#include "WorldObject.h"
#include "KDTree.h"
#include "windows.h"

template <class T>
class Spawner {
	public:
		Spawner(KDTree * tree, float delay = 1000.0f);

		void start();
		void stop();

		bool isActive();

	private:
		void spawnLoop();
		void spawn();
		bool active;
		bool enabled;
		float spawnTime;
		KDTree * theTree;
		~Spawner();
};

template <class T> Spawner<T>::Spawner(KDTree * tree, float delay) : active(false), spawnTime(delay)
{
	theTree = tree;
	/*
	T * spawnType = new T();
	if (dynamic_cast<WorldObject*>(spawnType))
	{
		enabled = true;
	}
	else
	{
		enabled = false;
		//~Spawner();
	}
	delete(spawnType);
	*/
}

template <class T>
Spawner<T>::~Spawner()
{

}

template <class T>
void Spawner<T>::start() {
	if (enabled)
		active = true;
	spawnLoop();
	//spawn();
}

template <class T>
void Spawner<T>::stop() {
	active = false;
}

template <class T>
bool Spawner<T>::isActive() {
	return active;
}

template <class T>
void Spawner<T>::spawn() {
	theTree->addObject(new T());
}

template <class T>
void Spawner<T>::spawnLoop() {
//	while (active) {
		spawn();
		//Sleep(spawnTime);
//	}
}